<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) 2015-2016 http://www.clcw.com.cn All rights reserved.    |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: Xmh <xiangminghu@clcw.com.cn>                                  |
// +------------------------------------------------------------------------+
//

namespace AuctionAppApi\Controller;

use AuctionCore\CarDealer\Dealer;
use AuctionCore\CarDealer\AssetCenter;
use AuctionCore\Other\Coupon;
use AuctionCore\System\AuctionException;
use AuctionCore\CarDealer\BuyCarList;
use AuctionCore\Other\XimuApi;
use AuctionCore\CarResource\Car;
use AuctionCore\AuctionOrder\AuOrder;
use AuctionCore\CarResource\CarModel;
use AuctionCore\Common\PhpRedis;

/**
 * 资产接口
 */
class AssetController extends BaseController {

    public function index(){

    }

    /**
     * 资产中心接口
     * @author angl
     */
    public function center()
    {
        $dealer_id = I('post.dealer_id/d',0);
        if($dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $d = new Dealer($dealer_id);
        $dealer_info = $d->dealer_base_info();
        $data = array();
        $data['total_amount'] = $dealer_info['bail_amount']+$dealer_info['freeze_amount']+$dealer_info['indemnity_amount'];;
        $data['remain_amount'] = $dealer_info['bail_amount'];//可用保证金总额
        $data['free_amount'] = $dealer_info['freeze_amount'];
        $data['paid_amount'] = $dealer_info['indemnity_amount'];
        $c = new Coupon($this->dealer_id);
        $coupon_stat = $c->coupon_stat();
        $data['coupon_money'] = $coupon_stat['cur_amount_sum'];
        $data['coupon_count'] = $c->get_coupon_count(array('type'=>1));//可使用
        $data['used_count'] = $c->get_coupon_count(array('type'=>2));;//已使用
        $data['past_count'] = $c->get_coupon_count(array('type'=>3));;//已过期
        $this->api_200($data);
    }

    /**
     * 代金券列表
     */
    public function coupon(){

        $page = I('post.page','1');
        $page_size = I('post.page_size','20');
        $dealer_id = $this->dealer_id;

        $type = I('post.type');//1：可使用，2：已使用，3：已过期
        $map = array();
        $map['dc.dealer_id'] = array('eq', $dealer_id);

        switch ($type) {
            case '1':
                $where = array();
                $where['dc.use_status'] = array('eq', '0');
                $where['dc.end_date'] = array('egt', date('Y-m-d'));
                $map['_complex'] = $where;
                break;
            case '2':
                $map['dc.use_status'] = array('eq', '1');
                break;
            case '3':
                $map['dc.use_status'] = array('neq', '1');
                $map['dc.end_date'] = array('lt', date('Y-m-d'));
                break;
            default:
                $where = array();
                $where['dc.use_status'] = array('eq', '0');
                $where['dc.end_date'] = array('egt', date('Y-m-d'));
                $map['_complex'] = $where;
                break;
        }
        $prefix = C("DB_PREFIX");
        //计算记录数
        $total = M("dealer_coupon")->alias("dc")
                ->join(" left join ".$prefix."coupon_type as ct on dc.coupon_type_id = ct.coupon_type_id")
                ->join(" left join ".$prefix."activity as a on a.aid = ct.a_id")
                ->join(" left join ".$prefix."lottery_coupon as lc on lc.coupon_id = dc.coupon_id")
                ->join(" left join ".$prefix."scratch_card as sc on sc.cid = lc.card_id")
                ->join(" left join ".$prefix."order as o on o.order_id = sc.order_id")
                ->where($map)->count();
        $offset = ($page-1)*$page_size;
        $limit_value = $offset . "," . $page_size;
        $res = M("dealer_coupon")->alias("dc")->field("dc.*,o.order_no,ct.type_name,a.type")
                ->join(" left join ".$prefix."coupon_type as ct on dc.coupon_type_id = ct.coupon_type_id")
                ->join(" left join ".$prefix."activity as a on a.aid = ct.a_id")
                ->join(" left join ".$prefix."lottery_coupon as lc on lc.coupon_id = dc.coupon_id")
                ->join(" left join ".$prefix."scratch_card as sc on sc.cid = lc.card_id")
                ->join(" left join ".$prefix."order as o on o.order_id = sc.order_id")
                ->where($map)->order("end_date")->limit($limit_value)->select();
        $list = array();
        foreach ($res as $key => $value) {
            $list[$key] = $value;
            $list[$key]['get_method'] = $value['coupon_type_name'];
            $list[$key]['get_date'] = $value['start_date'];
            if ($value['type'] == 2) {
                $list[$key]['coupon_source'] ="来源：". $value['type_name'].",".$value['order_no'];
            }else{
                $list[$key]['coupon_source'] ="来源：". $value['type_name'];
            }
        }

        $data = array();
        $data['currentpage'] = $page;
        $data['pagesize'] = $page_size;
        $data['maxcount'] = $total;
        $data['maxpage'] = ceil($total/$page_size);
        $data['list'] = $list;
        $this->api_200($data);

    }
    /**
     *使用代金券
     */
    public function use_coupon(){
        //获取数据
        $dealer_id = $this->dealer_id;
        $order_id = $this->order_id;
        $coupon_ids = I('post.coupon_ids');
        //数据验证
        if(!$dealer_id){
            AuctionException::make_throw('500001');
        }
        if(!$order_id){
            AuctionException::make_throw('700027');
        }
        if(!$coupon_ids){
            AuctionException::make_throw('800013');
        }

        //判断状态
        $order = new \AuctionCore\AuctionOrder\AuOrder($order_id);
        $order_info = $order->detail();
        if($order_info['dealer_pay_status']!=0 || $order_info['coupon']<=0){
            AuctionException::make_throw('800014');
        }
        //数据处理
        $dealer = new \AuctionCore\CarDealer\Dealer($dealer_id);
        $data = array();
        $data['dealer_id'] = $dealer_id;
        $data['order_id'] = $order_id;

        $data['coupon_ids'] = explode(',',$coupon_ids);
        $res = $dealer->do_cost_coupon($data);
        if(!$res){
            AuctionException::make_throw('800012');
        }
       /* $redis = new \AuctionCore\Common\PhpRedis(2); //记录用户修改后的代金券使用结果
        $redis_key = "coupon_id-" . $order_id;
        $redis->set($redis_key,$coupon_ids);*/
        $data = array();
        $this->api_200($data);

    }
    /**
     *使用代金券
     */
    public function use_coupon_v151(){
        //获取数据
        $dealer_id = $this->dealer_id;
        $order_id = $this->order_id;
        $redis = new PhpRedis(); //记录用户修改后的代金券使用结果
        //$redis_key = "coupon_id-". $order_id;
        $arr = [
            'order_id'=>$order_id,
        ];
        $redis_key = getRedisKeyByCode('REDIS_KEY','100000',$arr);
        $coupon_ids = $redis->get($redis_key);
        //数据验证
        if(!$dealer_id){
            AuctionException::make_throw('500001');
        }
        if(!$order_id){
            AuctionException::make_throw('700027');
        }
        if(!$coupon_ids){
            AuctionException::make_throw('800013');
        }

        //判断状态
        $order = new AuOrder($order_id);
        $order_info = $order->detail();
        if($order_info['dealer_pay_status']!=0 || $order_info['coupon']<=0){
            AuctionException::make_throw('800014');
        }
        //数据处理
        $dealer = new Dealer($dealer_id);
        $data = array();
        $data['dealer_id'] = $dealer_id;
        $data['order_id'] = $order_id;

        $data['coupon_ids'] = explode(',',$coupon_ids);
        $res = $dealer->do_cost_coupon($data);
        if(!$res){
            AuctionException::make_throw('800012');
        }
        $data = array();
        $this->api_200($data);
    }
    //记录已选中的代金券 --更改默认代金券
    public function record_coupon_v151(){
        $dealer_id = I('post.dealer_id');
        $order_id = I('post.order_id');
        $coupon_ids = I('post.coupon_ids',0);
        //数据验证
        if(!$dealer_id){
            AuctionException::make_throw('500001');
        }
        if(!$order_id){
            AuctionException::make_throw('700027');
        }
        $redis = new PhpRedis();
        $arr = ['order_id'=>$order_id];
        $redis_key_coupon = getRedisKeyByCode('REDIS_KEY','100000',$arr);
        $confirm_key = getRedisKeyByCode('REDIS_KEY','100001',$arr);
        $res = $redis->set($redis_key_coupon,$coupon_ids); //将默认代金券计入到redis中;
        $redis->set($confirm_key,'1');
        if($res){
            $this->api_200('');
        }else{
            AuctionException::make_throw('800012');
        }
    }

   //我的代金卷列表
    public function coupon_list(){
        $bc = new BuyCarList($this->dealer_id);
        $list = $bc->get_coupon_list();
        foreach ($list as $key => $value) {
           if ($value['type'] == 2) {
                $list[$key]['coupon_source'] ="来源：". $value['type_name'].",".$value['order_no'];
            }else{
                $list[$key]['coupon_source'] ="来源：". $value['type_name'];
            }
        }
        $data = array();
        $data['list'] = $list;
        $this->api_200($data);
    }
    //我的代金卷列表  --牛治鑫
    public function coupon_list_v151(){
        $order_id = I('post.order_id/d', 0);
        $bc = new BuyCarList($this->dealer_id);
        $list = $bc->get_coupon_list();
        foreach ($list as $key => $value) {
            if ($value['type'] == 2) {
                $list[$key]['coupon_source'] ="来源：". $value['type_name'].",".$value['order_no'];
            }else{
                $list[$key]['coupon_source'] ="来源：". $value['type_name'];
            }
        }
        $redis = new PhpRedis(); //给默认使用的代金券加上checked标记
        $arr = ['order_id'=>$order_id];
        $redis_key = getRedisKeyByCode('REDIS_KEY','100000',$arr);
        $default_coupon_ids = $redis->get($redis_key);
        $default_coupon_id = explode(',',$default_coupon_ids);
        foreach($list as $k => $v){
            if(in_array($v['coupon_id'],$default_coupon_id)){
                $list[$k]['checked'] = true;
            }
        }
        $data = array();
        $data['list'] = $list;
        $this->api_200($data);
    }
    /**
     * 保证金使用记录
     * @author angl
     */
    public function history_bail()
    {
        if($this->dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $page = I('post.page/d',1);
        $page_size = I('post.page_size/d',20);
        $params = array();
        $asset_center = new AssetCenter($this->dealer_id);
        //获取历史记录
        $result = $asset_center->get_bail_logs($page, $page_size, $params);
        $total = empty($result['total']) ? 0  : $result['total'] ;
        $list = empty($result['logs']) ? array() : $result['logs'];
        $data = array();
        $data['currentpage'] = $page;
        $data['pagesize'] = $page_size;
        $data['maxcount'] = $total;
        $data['maxpage'] = ceil($total/$page_size);
        $data['list'] = $list;
        $this->api_200($data);
    }
    /**
 * 保证金使用记录 客户端1
 * @author angl
 */
    public function history_bail_v100()
    {
        if($this->dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $page = I('post.page/d',1);
        $page_size = I('post.page_size/d',20);
        $params = array();
        $asset_center = new AssetCenter($this->dealer_id);
        //获取历史记录
        $result = $asset_center->get_bail_logs($page, $page_size, $params);
        foreach($result['logs'] as $k=>$v){
            switch($v['use_type']){
                case '-1':$v['use_type'] = '未知';break;
                case '1':$v['use_type'] = '充值';break;//充值
                case '2':$v['use_type'] = '冻结';break;//冻结出价保证金
                case '3':$v['use_type'] = '解冻';break;//解冻出价保证金
                case '4':$v['use_type'] = '提取';break;//提取
                case '5':$v['use_type'] = '扣除';break;//争议支出
                case '6':$v['use_type'] = '扣除';break;//违约赔付
            }
            $result['logs'][$k]=$v;
        }
        $total = empty($result['total']) ? 0  : $result['total'] ;
        $list = empty($result['logs']) ? array() : $result['logs'];
        $data = array();
        $data['currentpage'] = $page;
        $data['pagesize'] = $page_size;
        $data['maxcount'] = $total;
        $data['maxpage'] = ceil($total/$page_size);
        $data['list'] = $list;
        $this->api_200($data);
    }

    /**
     * 保证金使用记录 客户端1
     * @author angl
     */
    public function history_bail_v151()
    {
        if($this->dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $page = I('post.page/d',1);
        $page_size = I('post.page_size/d',20);
        $params = array();
        $asset_center = new AssetCenter($this->dealer_id);
        //获取历史记录
        $result = $asset_center->get_bail_logs($page, $page_size, $params);
        foreach($result['logs'] as $k=>$v){
            $result['logs'][$k]=$v;
            $result['logs'][$k]['occur_money']=floor($v['occur_money']);
            $result['logs'][$k]['remain_amount']=floor($v['remain_amount']);
            switch($v['use_type']){
                case '-1':$result['logs'][$k]['use_type_name'] = '未知';break;
                case '1':$result['logs'][$k]['use_type_name'] = '充值';break;//充值
                case '2':$result['logs'][$k]['use_type_name'] = '冻结';break;//冻结出价保证金
                case '3':$result['logs'][$k]['use_type_name'] = '解冻';break;//解冻出价保证金
                case '4':$result['logs'][$k]['use_type_name'] = '提取';break;//提取
                case '5':$result['logs'][$k]['use_type_name'] = '扣除';break;//争议支出
                case '6':$result['logs'][$k]['use_type_name'] = '扣除';break;//违约赔付
                case '8':$result['logs'][$k]['use_type_name'] = '扣除';break;//维保支出
            }

        }
        $total = empty($result['total']) ? 0  : $result['total'] ;
        $list = empty($result['logs']) ? array() : $result['logs'];
        $data = array();
        $data['currentpage'] = $page;
        $data['pagesize'] = $page_size;
        $data['maxcount'] = $total;
        $data['maxpage'] = ceil($total/$page_size);
        $data['list'] = $list;
        $this->api_200($data);
    }

    /**
     * 资产中心接口 center_v130
     */
    public function center_v130()
    {   
        $dealer_id = I('post.dealer_id/d',$this->dealer_id);
        if($dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $d = new Dealer($dealer_id);
        $dealer_info = $d->dealer_base_info();
        $dealer_credit_info = $d ->get_dealer_credit();
        $data = array();
        $data['total_amount'] = $dealer_info['bail_amount']+$dealer_info['freeze_amount']+$dealer_info['indemnity_amount'];;
        $data['remain_amount'] = $dealer_info['bail_amount'];//可用保证金总额
        $data['free_amount'] = $dealer_info['freeze_amount'];
        $data['paid_amount'] = $dealer_info['indemnity_amount'];
        $c = new Coupon($this->dealer_id);
        $coupon_stat = $c->coupon_stat();
        $data['coupon_money'] = $coupon_stat['cur_amount_sum'];
        $data['coupon_count'] = $c->get_coupon_count(array('type'=>1));//可使用
        $data['used_count'] = $c->get_coupon_count(array('type'=>2));;//已使用
        $data['past_count'] = $c->get_coupon_count(array('type'=>3));;//已过期
        // $ximu = new XimuApi();
        // //可用消费额度
        // $data2 = array();
        // $data2['product_id'] = C('PROD_CAR_OLD_CLCW.0');
        // $data2['out_user_code'] = "20160804";

        // $amountUsed = $ximu->_get("CREDIT","queryallowbal",$data2);
        // $data['crediLine'] = ($amountUsed['data']['available_bal'] + $amountUsed['data']['total_spending'])/100;//信贷额度总值
        // $data['availableCredit'] = $amountUsed['data']['available_bal']/100;//可用信贷额度
        // $data['amountUsed'] = $amountUsed['data']['total_spending']/100;//已经使用的信贷
        // $data['is_credit'] = $dealer_credit_info['is_credit']; //是否授信
        $this->api_200($data);
    }

        /**
     * 资产中心接口 center_v140
     */
    public function center_v140()
    {   
        $dealer_id = I('post.dealer_id/d',$this->dealer_id);
        if($dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $d = new Dealer($dealer_id);
        $dealer_info = $d->dealer_base_info();
        $dealer_credit_info = $d ->get_dealer_credit();
        $data = array();
        $data['total_amount'] = $dealer_info['bail_amount']+$dealer_info['freeze_amount']+$dealer_info['indemnity_amount'];;
        $data['remain_amount'] = $dealer_info['bail_amount'];//可用保证金总额
        $data['free_amount'] = $dealer_info['freeze_amount'];
        $data['paid_amount'] = $dealer_info['indemnity_amount'];
        $data['minus_amount'] = $dealer_info['minus_amount'];  //维保透资额度
        $c = new Coupon($this->dealer_id);
        $coupon_stat = $c->coupon_stat();
        $data['coupon_money'] = $coupon_stat['cur_amount_sum'];
        $data['coupon_count'] = $c->get_coupon_count(array('type'=>1));//可使用
        $data['used_count'] = $c->get_coupon_count(array('type'=>2));;//已使用
        $data['past_count'] = $c->get_coupon_count(array('type'=>3));;//已过期
        if ($dealer_credit_info['is_credit'] == 1) {
            $ximu = new XimuApi();
            //可用消费额度
            $data2 = array();
            $data2['product_id'] = C('PROD_CAR_OLD_CLCW.0');
            $data2['out_user_code'] = $dealer_id;

            $amountUsed = $ximu->_get("CREDIT","queryallowbal",$data2);
            $data['creditLine'] = ($amountUsed['data']['available_bal'] + $amountUsed['data']['total_spending'])/100;//信贷额度总值a
            $data['availableCredit'] = $amountUsed['data']['available_bal']/100;//可用信贷额度
            $data['amountUsed'] = $amountUsed['data']['total_spending']/100;//已经使用的信贷
            $data['is_credit'] = $dealer_credit_info['is_credit']; //是否授信
        }
        $this->api_200($data);
    }


    /**
    * 贷款历史 16-08-18
    */
    public function loan_history(){
        $dealer_id = I('post.dealer_id/d',$this->dealer_id);
        if($dealer_id<1){
            AuctionException::make_throw('500001');
        }
        $page = I('post.page','1');
        $page_size = I('post.page_size','20');
        $data = array();
        $order_no = I('post.order_no');
        if ($order_no) {
            $data['out_list_code'] = $order_no;
        }
        $xm_list_code = I('post.xm_list_code');
        if ($xm_list_code) {
            $data['xm_list_code'] = $xm_list_code;
        }
        $start_time = I('post.start_time');
        if ($start_time) {
            $data['start_time'] = $start_time;
        }
        $end_time = I('post.end_time');
        if ($end_time) {
            $data['end_time'] = $end_time;
        }
        $loan_type = I('post.state/d',"0");
        if ($loan_type >= 0) {
            $data['state'] = (string)$loan_type;
        }
        $ximu = new XimuApi();
        $data['product_id'] = C('PROD_CAR_OLD_CLCW.0');
        $data['out_user_code'] = $dealer_id;
        $data['page_num'] = $page;
        $data['page_size'] = $page_size;
        $res = $ximu->_get("ORDER","querylist",$data);
        $list = $res['data']['list'];
        // dump($res);
        $result = array();
        $status_type = array(
        '1'=>array('WAIT_DOWN_PAYMENT_CONFIRM','WAIT_LOAN_CONFIRM','WAIT_APPLY_APPROVE'),
        '2'=>array('ALREADY_LOAN','WAIT_COLLECTION_CONFIRM'),
        '3'=>array('ALREADY_OVERDUE'),
        '4'=>array('ALREADY_PAYMENT'),
        '5'=>array('CANCEL')
        );
        // dump($list);
        // die();
        foreach ($list as $key => $value) {
            $order_info = array();
            $car_info = array();
            $o = new AuOrder($value['out_list_code']);
            $order_info = $o->detail();
            // var_dump($order_info);
            // die();
            $car = new Car($order_info['car_id']);
            $car_info = $car->detail();
            $cm = new CarModel();
            $a = new \AuctionCore\Other\Address();
            $city_name = $a->get_name_by_code($car_info['location_area']);
            $series = $cm->get_model_by_id($car_info['series_id'], 'series');
            $car_model = '['.$city_name.'] '.$series['name'] . ' ' . $car_info['car_cc'] . ($car_info['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_info['gearbox']);
            $img = Car::get_pic_by_key($order_info['car_id'], "p_left_front45");//左前45°图片 
            $result[$key]['loanSN'] = $value['xm_list_code'];
            $result[$key]['borrowingBalance'] = "申请贷款金额：".($value['apply_bal']/100)."元";
            $result[$key]['applyTime'] = $value['apply_time'];  
            foreach ($status_type as $k => $v) {
                if (in_array($value['list_stat'],$v)) {
                    $state_flag = $k;
                }
            }
            $result[$key]['applyState'] = $state_flag; //申请状态的名称
            $result[$key]['applyStateName'] = C('LOAN_STATE')[$state_flag]; //申请状态的名称
            $result[$key]['title'] = $car_model;
            $result[$key]['Img'] = $img['pic_url'];
        }

        $data = array();
        $data['currentpage'] = $res['data']['page'];
        $data['pagesize'] = $res['data']['items_per_page'];
        $data['maxcount'] = $res['data']['items'];
        $data['maxpage'] = ceil($res['data']['items']/$res['data']['items_per_page']);
        $data['list'] = $result;
        $this->api_200($data);
    }

    /**
    * 贷款详情 16-08-18
    */
    public function loan_detail(){
        $loanSN = I('post.loanSN',0);
        if(!$loanSN){
            AuctionException::make_throw('1700000');
        }
        $dealer_id = I('post.dealer_id/d',$this->dealer_id);
        $dealer_id = $dealer_id;
        $page = I('post.page','1');
        $page_size = I('post.page_size','20');
        $data = array();
        $ximu = new XimuApi();
        $data['product_id'] = C('PROD_CAR_OLD_CLCW.0');
        $data['out_user_code'] = $dealer_id;
        $data['page_num'] = $page;
        $data['page_size'] = $page_size;
        $data['xm_list_code'] = (string)$loanSN;
        $data['state'] = "0";
        $res = $ximu->_get("ORDER","querylist",$data);
        $list = $res['data']['list'][0];
// dump($list);
        // die();
        //贷款单号所处状态
        $status_type = array(
        '1'=>array('WAIT_DOWN_PAYMENT_CONFIRM','WAIT_LOAN_CONFIRM','WAIT_APPLY_APPROVE'),
        '2'=>array('ALREADY_LOAN','WAIT_COLLECTION_CONFIRM'),
        '3'=>array('ALREADY_OVERDUE'),
        '4'=>array('ALREADY_PAYMENT'),
        '5'=>array('CANCEL')
        );
        $state_flag = '';
         foreach ($status_type as $k => $v) {
            if (in_array($list['list_stat'],$v)) {
                $state_flag = $k;
            }
        }
        $state_name = C('LOAN_STATE')[$state_flag]; //申请状态的名称
        $order_info = array();
        $car_info = array();
        $o = new AuOrder($list['out_list_code']);
        $order_info = $o->detail();
        $car = new Car($order_info['car_id']);
        $car_info = $car->detail();
        $cm = new CarModel();
        $a = new \AuctionCore\Other\Address();
        $city_name = $a->get_name_by_code($car_info['location_area']);
        $series = $cm->get_model_by_id($car_info['series_id'], 'series');
        $car_model = '['.$city_name.'] '.$series['name'] . ' ' . $car_info['car_cc'] . ($car_info['turbo'] ? 'T' : 'L') . ' ' . C('GEARBOX.' . $car_info['gearbox']);
        $img = Car::get_pic_by_key($order_info['car_id'], "p_left_front45");//左前45°图片 
        $result = array();
        $result['content'][] = array(
                "type"=>"header",
                "car_name"=>$car_model,
                "img"=>$img['pic_url'],
                "summary"=>$loanSN,
                "should_pay"=>'申请贷款金额：'.($list['apply_bal']/100).'元'
            );
        
        $newdata = array();
        $newdata['product_id'] = C('PROD_CAR_OLD_CLCW.0');
        $newdata['out_user_code'] = $dealer_id;
        $newdata['xm_list_code'] = (string)$loanSN;
        // $newdata['page_num'] = $page;
        // $newdata['page_size'] = $page_size;
        // dump($newdata);
        $res1 = $ximu->_get("ORDER","queryreturndetail",$newdata);

        if ($list['end_date']) {
            if ($state_flag == 3) {
                $overdue_day = ceil((time()-strtotime($list['end_date']))/86400) > 0 ? ceil((time()-strtotime($list['end_date']))/86400) : 0;
            }elseif ($state_flag == 4) {
                $overdue_day = ceil((strtotime($res1['data']['list'][0]['r_date'])-strtotime($list['end_date']))/86400) > 0 ? ceil((strtotime($res1['data']['list'][0]['r_date'])-strtotime($list['end_date']))/86400) : 0;
            }
        }

        $items[] = array('type'=>'name_value','name' => '贷款编号', 'value' =>$list['xm_list_code']);
        $items[] = array('type'=>'name_value','name' => '拍单编号', 'value' =>$order_info['order_no']);
        $items[] = array('type'=>'name_value','name' => '申请贷款金额', 'value' =>$list['apply_bal']/100 ."元");
        $items[] = array('type'=>'name_value','name' => '贷款期限', 'value' =>$list['loan_period']."天");
        $items[] = array('type'=>'name_value','name' => '申请时间', 'value' =>$list['apply_time']);
        if ($state_flag == 2) {
            $items[] = array('type'=>'name_value','name' => '贷款费用', 'value' =>($list['charge_amt']/100) ."元");
            $items[] = array('type'=>'name_value','name' => '放款时间', 'value' =>date('Y-m-d', strtotime($list['loan_date'])));
            $items[] = array('type'=>'name_value','name' => '到期时间', 'value' =>date('Y-m-d', strtotime($list['end_date'])));
        }elseif ($state_flag == 3) {
            $items[] = array('type'=>'name_value','name' => '贷款费用', 'value' =>($list['charge_amt']/100) ."元");
            $items[] = array('type'=>'name_value','name' => '放款时间', 'value' =>date('Y-m-d', strtotime($list['loan_date'])));
            $items[] = array('type'=>'name_value','name' => '到期时间', 'value' =>date('Y-m-d', strtotime($list['end_date'])));
            $items[] = array('type'=>'name_value','name' => '逾期天数', 'value' =>$overdue_day."天");
            $items[] = array('type'=>'name_value','name' => '罚息', 'value' =>($res1['data']['list'][0]['sa_fine']-$res1['data']['list'][0]['ra_fine'])/100 ."元");
        }elseif ($state_flag == 4) {
            $items[] = array('type'=>'name_value','name' => '贷款费用', 'value' =>($list['charge_amt']/100) ."元");
            $items[] = array('type'=>'name_value','name' => '放款时间', 'value' =>date('Y-m-d', strtotime($list['loan_date'])));
            $items[] = array('type'=>'name_value','name' => '到期时间', 'value' =>date('Y-m-d', strtotime($list['end_date'])));
            $items[] = array('type'=>'name_value','name' => '还款时间', 'value' =>date('Y-m-d', strtotime($res1['data']['list'][0]['r_date'])));
            $items[] = array('type'=>'name_value','name' => '逾期天数', 'value' =>$overdue_day."天");
            $items[] = array('type'=>'name_value','name' => '罚息', 'value' =>$res1['data']['list'][0]['ra_fine']/100 ."元");
            $items[] = array('type'=>'name_value','name' => '还款总额', 'value' =>($res1['data']['list'][0]['apply_bal']+$res1['data']['list'][0]['s_fine']+$res1['data']['list'][0]['ra_fine'])/100 ."元");
        }elseif ($state_flag == 5) {
            $loan_log = AuOrder::get_loan_log($loanSN);
            $items[] = array('type'=>'name_value','name' => '拒贷时间', 'value' =>$loan_log['canceltime']);
        }
        $items[] = array('type'=>'name_value','name' => '贷款状态', 'value' =>$state_name);
        $result['content'][] = array(
                "type"=>"group",
                "title"=>"贷款信息",
                "items"=>$items
            );
        // dump($result);
        $this->api_200($result);
    }

    /**
    * 贷款取消 16-08-18
    */
    public function cancel_loan(){
        $loanSN = I('post.loanSN',0);
        if(!$loanSN){
            AuctionException::make_throw('1700000');
        }
        $loan_log = AuOrder::get_loan_log($loanSN);
        if (!$loan_log) {
            AuctionException::make_throw('1700001');
        }
        $o = new AuOrder($loan_log['order_id']);
        //贷款前置状态判断
        $data = array();
        $data['xm_list_code'] = (string)$loanSN;
        $data['product_id'] = C('PROD_CAR_OLD_CLCW.0');
        $ximu = new XimuApi();
        $res = $ximu->_get("ORDER","cancellist",$data);
        if ($res['code'] == 200) {
            $res1 = $o->cancel_loan();
            if ($res1<1) {
                AuctionException::make_throw('100066');
            }else{
                $this->api_200($res1);
            }
        }
    }

    
}
//end class